package com.kvn.strategy;

import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * Created by wangzhiyuan on 2018/6/7
 */
@Component
public class ModShardStrategyImpl<T extends Number> implements ShardStrategy<T> {
    @Override
    public String decideTableName(Number shardColumnValue, String strategy) {
        int mod = Integer.valueOf(strategy.substring(1));
        return String.valueOf(shardColumnValue.longValue() % mod);
    }

    @Override
    public boolean apply(String strategy) {
        Pattern pattern = Pattern.compile("^%\\d+$");
        return pattern.matcher(strategy).find();
    }

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("^%\\d+$");
        String strategy = "%32";
        boolean b = pattern.matcher(strategy).find();
        System.out.println("args = [" + b + "]");
    }
}
